"""
@Project ：Python_learning 
@File    ：999. 可以被一步捕获的棋子数.py
@Author  ：zhupp
@Date    ：2024/12/8 15:46 
"""
from typing import List

"""
给定一个 8 x 8 的棋盘，只有一个 白色的车，用字符 'R' 表示。棋盘上还可能存在白色的象 'B' 以及黑色的卒 'p'。空方块用字符 '.' 表示。

车可以按水平或竖直方向（上，下，左，右）移动任意个方格直到它遇到另一个棋子或棋盘的边界。如果它能够在一次移动中移动到棋子的方格，则能够 吃掉 棋子。

注意：车不能穿过其它棋子，比如象和卒。这意味着如果有其它棋子挡住了路径，车就不能够吃掉棋子。

返回白车 攻击 范围内 兵的数量。
"""


"""
心得：在二维数组board[x][y]中，x表示行，y表示列，而在坐标系中，恰恰相反。 
       for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]：代表了左右上下四个方向
            while 0 <= x < size and 0 <= y < size and board[x][y] == '.':
                x += dx
                y += dy
       让某一方向上的位置加一或者减一。
"""

class Solution:
    def numRookCaptures(self, board: List[List[str]]) -> int:
        size = len(board)
        for i in range(len(board)):    # 列数
            for item in enumerate(board[i]):  # 行数
                # 找到R在第几行第几列
                if item[1] == 'R':
                    x0, y0 = i, item[0]
                    print(x0, y0)
        ans = 0
        # 往R的四个方向遍历
        for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            x, y = x0 + dx, y0 + dy
            # 如果不是遍历的时候没以后遇到p或者B就继续走，直到边界
            while 0 <= x < size and 0 <= y < size and board[x][y] == '.':
                x += dx
                y += dy
            # 如果遇到p就次数加一
            if 0 <= x < size and 0 <= y < size and board[x][y] == 'p':
                ans += 1
        return ans



s = Solution()
print(s.numRookCaptures([[".",".",".",".",".",".",".","."],
                         [".",".",".","p",".",".",".","."],
                         [".",".",".","R",".",".",".","p"],
                         [".",".",".",".",".",".",".","."],
                         [".",".",".",".",".",".",".","."],
                         [".",".",".","p",".",".",".","."],
                         [".",".",".",".",".",".",".","."],
                         [".",".",".",".",".",".",".","."]]))